// 路由鉴权
import router from './router'
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import useUserStore from '@/store/modules/user'

const whitePath: Array<string> = [
  '/home',
  '/hospital/register',
  '/hospital/detail',
  '/hospital/notice',
  '/hospital/close',
  '/hospital/search'
]
// 前置路由
Nprogress.configure({
  showSpinner: false
})
router.beforeEach((to, _, next) => {
  Nprogress.start({
    showSpinner: false
  })
  document.title = to.meta.title ? '商医通 - ' + to.meta.title : '商医通'
  // 获取token
  let userStore = useUserStore()
  const token = userStore.userInfo.token
  // // 判断是否登录
  if (token) {
    // 已登录
    next()
  } else {
    if (whitePath.indexOf(to.path) != -1) {
      next()
    } else {
      next('/home?redirect=' + to.fullPath)
      userStore.visiable = true
    }
  }
})

router.afterEach(() => {
  Nprogress.done()
})
